<?php
// +----------------------------------------------------------------------
// * Created by PhpStorm.
// +----------------------------------------------------------------------
// * Author: D H
// +----------------------------------------------------------------------
// * Date: 2017/9/12
// +----------------------------------------------------------------------
// * Time: 10:06
// +----------------------------------------------------------------------
namespace app\api\logic;
use think\Db;
use think\Session;
use tool\Common;
class Supply extends Base{
    //+---------------------------------------------------
    //*协议供货 首页展示
    //+---------------------------------------------------
    public function agreementIndex(){
        $data = $this->data;
        $data = [
            'catID' =>14,
            'brandID' => 1
        ];
        $user = Session::get('userinfo');
        $pid = Db::name('user')->field('parentID')->where('id',$user['userID'])->find();
        $selID = Db::name('team_select')->field(['id' => 'selectID'])->where('teamID',$pid['parentID'])->select();
        $id = [];
        foreach ($selID as $k => $val){
            $id[$k]=$val['selectID'];
        }
        $sql = Db::view('team_select_item','productID')
            ->view('product',['name' => 'productName','marketsPrice'],'team_select_item.productID = product.id')
            ->view('product_content',['titleImage'],'product.id = product_content.productID')
            ->view('user',['sellerName','logo'],'product.sellerID = user.id')
            ->view('brand',['id' => 'brandID','name' => 'brandName'],'product.brandID = brand.id')
            ->where('team_select_item.selectID','in',$id);
        if (array_key_exists('brandID',$data)){
            $list = $sql ->where('product.catalogID',$data['catID'])
                ->where('product.brandID',$data['brandID'])
                ->select();
        }else{
            $list = $sql ->where('product.catalogID',$data['catID'])
                ->select();
        }
        $proList = [];
        $proIDS = [];
        //产品列表
        foreach ($list as $k => $val){
            $proList[$k]['titleImage'] = json_decode($val['titleImage']);
            $proList[$k]['titleImage'] = $proList[$k]['titleImage'][0];
            $proList[$k]['name'] = $val['brandName'].'/'.$val['productName'];
            $proList[$k]['sellerName'] = $val['sellerName'];
            $proList[$k]['marketsPrice'] = $val['marketsPrice'];
            //产品ID
            $proIDS[$k] = $val['brandID'];
        }
        //品牌列表
        $brandList = Db::name('brand')
            ->field(['id' => 'brandID','name' => 'brandName'])
            ->where('id','in',$proIDS)
            ->distinct(true)
            ->select();
        return Common::rm(1,'操作成功',[
            //'total' => count($total),
           // 'catList' => $catList,
            'brand' => $brandList,
            'productList' => $proList
        ]);
    }
}